package com.kpi.myfloatwindow;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ImageView;


/**
 * 整体逻辑：
 * 1、请求权限
 * 2、请求权限成功后显示悬浮窗
 */
public class MainActivity extends AppCompatActivity {

    private View mView;
    private float downX;
    private float downY;
    private float upX;
    private float upY;
    private FloatPhone floatPhone;
    private boolean mClick = false;
    private ImageView imageView;
    private int[] images = {R.mipmap.icon_1_cover, R.mipmap.icon_2_cover, R.mipmap.icon_3_cover, R.mipmap.icon_4_cover, R.mipmap.icon_5_cover, R.mipmap.icon_6_cover,
            R.mipmap.icon_7_cover, R.mipmap.icon_8_cover, R.mipmap.icon_9_cover, R.mipmap.icon_10_cover,R.mipmap.icon_11_cover,R.mipmap.icon_12_cover,R.mipmap.icon_13_cover,
            R.mipmap.icon_14_cover,R.mipmap.icon_15_cover,R.mipmap.icon_16_cover};
    private int index = 1;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //在系统设置里面对本应用进行设置悬浮窗权限
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, 10);

        //悬浮窗视图初始化
        mView = getLayoutInflater().inflate(R.layout.floatwindow_view, null);
        imageView = mView.findViewById(R.id.floatimg);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10){
            show();
        }
    }

    //显示悬浮窗
    private void show(){
        floatPhone = new FloatPhone(getApplicationContext());
        floatPhone.setView(mView);
        floatPhone.init();//显示悬浮窗
        move();
        handler.postDelayed(runnable, 500);//执行每隔500毫秒执行切换一次图片
    }

    //切换图片的子线程
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Log.i("测试", "切换颜色: "+index);
            if (index == images.length){
                index = 0;
            }
            imageView.setImageResource(images[index]);
            index++;
            handler.postDelayed(runnable, 500);
        }
    };

    //使悬浮窗跟随手指移动
    private void move(){
        mView.setOnTouchListener(new View.OnTouchListener() {
            float lastX, lastY, changeX, changeY;
            int newX, newY;

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        downX = event.getRawX();
                        downY = event.getRawY();
                        lastX = event.getRawX();
                        lastY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        changeX = event.getRawX() - lastX;
                        changeY = event.getRawY() - lastY;
                        newX = (int) (floatPhone.getX() + changeX);
                        newY = (int) (floatPhone.getY() + changeY);
                        floatPhone.updateXY(newX, newY);
                        lastX = event.getRawX();
                        lastY = event.getRawY();
                        break;
                    default:
                        break;
                }
                return mClick;
            }
        });
    }
}